const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function savePicToAlbum(tempFilePath) {
  let that = this;
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.writePhotosAlbum']) {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success() {
            wx.saveImageToPhotosAlbum({
              filePath: tempFilePath,
              success(res) {
                wx.showToast({
                  title: '保存成功'
                });
              },
              fail(res) {
                console.log(res);
              }
            })
          },
          fail() {
            // 用户拒绝授权,打开设置页面
            wx.openSetting({
              success: function (data) {
                console.log("openSetting: success");
              },
              fail: function (data) {
                console.log("openSetting: fail");
              }
            });
          }
        })
      } else {
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
            });
          },
          fail(res) {
            console.log(res);
          }
        })
      }
    },
    fail(res) {
      console.log(res);
    }
  })
}

function image() {
  var imageSize = {};
  var originalScale = 0.2;//图片高宽比  
  //获取屏幕宽高  
  wx.getSystemInfo({
    success: function (res) {
      var windowWidth = res.windowWidth;
      var windowHeight = res.windowHeight;
      var windowscale = windowHeight / windowWidth;//屏幕高宽比  
      console.log('windowWidth: ' + windowWidth)
      console.log('windowHeight: ' + windowHeight)
      if (originalScale < windowscale) {//图片高宽比小于屏幕高宽比  
        //图片缩放后的宽为屏幕宽  
        imageSize.imageWidth = windowWidth;
        imageSize.imageHeight = (windowWidth * 400) / 320;
        imageSize.chaWidth = windowWidth - 320;
        imageSize.chaHeight = (windowWidth * 400) / 320 - 400;
        imageSize.imageTop = (windowHeight - imageSize.imageWidth) / 2;
      } else {//图片高宽比大于屏幕高宽比  
        //图片缩放后的高为屏幕高  
        imageSize.imageHeight = windowHeight;
        imageSize.imageWidth = (windowHeight * 320) / 400;
        imageSize.chaWidth = windowWidth - 320;
        imageSize.chaHeight = (windowWidth * 400) / 320 - 400;
      }

    }
  })
  console.log('缩放后的宽: ' + imageSize.imageWidth)
  console.log('缩放后的高: ' + imageSize.imageHeight)
  console.log("图片的高度" + imageSize.imageTop)
  return imageSize;
}


function image2(width, height) {
  var imageSize = {};
  var originalScale = 0.2;//图片高宽比  
  //获取屏幕宽高  
  wx.getSystemInfo({
    success: function (res) {
      var windowWidth = res.windowWidth;
      var windowHeight = res.windowHeight;
      var windowscale = windowHeight / windowWidth;//屏幕高宽比  

      if (originalScale < windowscale) {//图片高宽比小于屏幕高宽比  
        //图片缩放后的宽为屏幕宽  
        imageSize.imageWidth = windowWidth;
        imageSize.imageHeight = (windowWidth * 400) / 320;
        imageSize.chaWidth = windowWidth - 320;
        imageSize.chaHeight = (windowWidth * 400) / 320 - 400;
        imageSize.imageTop = (windowHeight - imageSize.imageWidth) / 2;
      } else {//图片高宽比大于屏幕高宽比  
        //图片缩放后的高为屏幕高  
        imageSize.imageHeight = windowHeight;
        imageSize.imageWidth = (windowHeight * 320) / 400;
        imageSize.chaWidth = windowWidth - 320;
        imageSize.chaHeight = (windowWidth * 400) / 320 - 400;
      }

    }
  })
  console.log('缩放后的宽: ' + imageSize.imageWidth)
  console.log('缩放后的高: ' + imageSize.imageHeight)
  console.log("图片的高度" + imageSize.imageTop)
  return imageSize;
}

function loading() {
  wx.showLoading({
    title: '玩命加载中...',
  })
  setTimeout(function () {
    wx.hideLoading();
  }, 300)
}

function countdown(that, hours, minutes, seconds) {
  var times = hours * 3600 + minutes * 60 + seconds * 1;
  var hour = 0, minute = 0, second = 0;

  var timer = setInterval(function () {
    if (times > 0) {
      times--;
      hour = Math.floor(times / (60 * 60));
      minute = Math.floor(times / 60) - (hour * 60);
      second = Math.floor(times) - (hour * 60 * 60) - (minute * 60);
      if (hour <= 9) hour = '0' + hour;
      if (minute <= 9) minute = '0' + minute;
      if (second <= 9) second = '0' + second;
      that.setData({
        hours: hour,
        minutes: minute,
        seconds: second

      });
    } else {


      clearInterval(timer);
      wx.showModal({
        title: '很遗憾，该商品砍价失败，继续努力！',
        content: '',
        cancelColor: "#666",
        cancelText: "我知道了",
        confirmColor: "#ff1313",
        confirmText: "重砍一个",
        fail: function () {

        },
        success: function (res) {
          if (res.confirm) {
            wx.navigateBack({
              delta: 1
            })

          }
          if (res.cancel) {
            wx.redirectTo({
              url: '/pages/kanjian/kanjian?key=' + 2,
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })
          }

        }
      })
      return;


    }

  }, 1000)




}





var hostUrl = "http://192.168.31.236/small/";
module.exports = {
  formatTime: formatTime,
  savePicToAlbum: savePicToAlbum,
  image: image,//获取图片缩放比函数

  loading: loading,//页面提示正在加载中
  countdown: countdown,//页面倒计时

  jktc_logo: "http://ydsuda.youdang.xin/pic/link.png",
 
  
  baseImgUrl: "/pages/images/",//服务器图片地址，公共部分
  getNewsDetail: hostUrl + "shouxq/index",
  getTuijianNews: hostUrl + "shouxq/tuijian",

  shoueyNav: hostUrl + "index/index",//获取首页导航栏和初始的数据
  shouyeClasses:hostUrl+"pindao/index",//获取分类的弹出
  zuopinIndex: hostUrl +"zuopin/zuopin_info" ,//获取新闻
  guanzhu: hostUrl + "zuopin/guanzhu",//新闻明细加关注
  quguan: hostUrl + "zuopin/guanzhu_del",//新闻明细取消关注
  shoucang: hostUrl + "zuopin/shoucang",//新闻明细加收藏
  qu_shoucang: hostUrl + "zuopin/shoucang_del",//新闻明细取消收藏

  tuijian_newslist: hostUrl + "index/get_zuopin",//首页推荐下的新闻列表
  audio_detail: hostUrl + "shouyevideo/index",//视频新闻明细
  tuijian_audio: hostUrl + "shouyevideo/tuijian",//视频新闻推荐

  pindao_my: hostUrl + "pindao/index",//我的频道
  pindao_qita: hostUrl + "shouyeclass/jikeclass",//其他频道
  add_pindao: hostUrl + "pindao/addclass",//添加频道//
  del_pindao: hostUrl + "pindao/delclass",//删除频道
}
